﻿Public Class frm_ftp_server

    Public Event UpdateFTPServers()

    Private _ftp_server As ConnectionInfo
    Private _mod As Boolean

    Public WriteOnly Property SetFTPServer As ConnectionInfo
        Set(value As ConnectionInfo)
            _ftp_server = value
        End Set
    End Property

    Public WriteOnly Property SetMod As Boolean
        Set(value As Boolean)
            _mod = value
        End Set
    End Property


    Private Sub cmd_save_Click(sender As Object, e As EventArgs) Handles cmd_save.Click

        Dim _ftp_server As New ConnectionInfo
        Dim _tools As New Tools
        Dim _filename As String = String.Empty

        Try

            If String.IsNullOrEmpty(txt_ftp_server_name.Text) Then
                Throw New Exception("Du musst einen Namen angeben!")
            End If

            If String.IsNullOrEmpty(txt_ftp_server_host.Text) Then
                Throw New Exception("Du musst einen Host angeben!")
            End If

            _filename = IO.Path.Combine(Application.StartupPath, "FTPServer" & IO.Path.DirectorySeparatorChar & txt_ftp_server_name.Text.ToLower.ToString & ".xml")

            _ftp_server.GetSetName = txt_ftp_server_name.Text

            _ftp_server.GetSetHost = txt_ftp_server_host.Text
            _ftp_server.GetSetPort = num_ftp_server_port.Value
            _ftp_server.GetSetPath = txt_ftp_server_path.Text
            _ftp_server.GetSetSecurityProtocol = FTP.FTP.FtpSecurityProtocol.None
            _ftp_server.GetSetTransferMode = _tools.ParseMode(comb_ftp_server_mode.Text.ToString)
            _ftp_server.GetSetTransferType = _tools.ParseTransferMode(comb_ftp_server_transfer_mode.Text.ToString)
            _ftp_server.GetSetCharacterEncoding = _tools.ParseEncoding(comb_ftp_server_encoding.Text.ToString)


            _ftp_server.GetSetAuthRequired = chk_auth_required.Checked
            _ftp_server.GetSetUsername = txt_ftp_server_user.Text
            _ftp_server.GetSetPassword = txt_ftp_server_password.Text

            _ftp_server.GetSetUseSpecialCharacterFix = chk_use_special_character_fix.Checked

            _tools.XMLSerialize(_ftp_server, _filename)

            MessageBox.Show("FTP Server erfolgreich hinzugefügt!", "FTP Server hinzugefügt", MessageBoxButtons.OK, MessageBoxIcon.Information)

            RaiseEvent UpdateFTPServers()

            Me.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Fehler beim hinzufügen des Servers", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub chk_auth_required_CheckedChanged(sender As Object, e As EventArgs) Handles chk_auth_required.CheckedChanged
        If chk_auth_required.Checked = True Then
            txt_ftp_server_user.Enabled = True
            txt_ftp_server_password.Enabled = True
        Else
            txt_ftp_server_user.Enabled = False
            txt_ftp_server_password.Enabled = False
        End If
    End Sub

    Private Sub frm_ftp_server_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        If _mod = True Then

            txt_ftp_server_name.Text = _ftp_server.GetSetName

            txt_ftp_server_name.ReadOnly = True


            txt_ftp_server_host.Text = _ftp_server.GetSetHost
            num_ftp_server_port.Value = _ftp_server.GetSetPort
            txt_ftp_server_path.Text = _ftp_server.GetSetPath


            If _ftp_server.GetSetTransferMode = FTP.FTP.TransferMode.Active Then
                comb_ftp_server_mode.SelectedIndex = 0
            Else
                comb_ftp_server_mode.SelectedIndex = 1
            End If

            If _ftp_server.GetSetTransferType = FTP.FTP.TransferType.Binary Then
                comb_ftp_server_transfer_mode.SelectedIndex = 0
            Else
                comb_ftp_server_transfer_mode.SelectedIndex = 1
            End If

            Select Case _ftp_server.GetSetCharacterEncoding

                Case Enums.CharacterEncoding.Standard

                    comb_ftp_server_encoding.SelectedIndex = 0

                Case Enums.CharacterEncoding.UTF8

                    comb_ftp_server_encoding.SelectedIndex = 1

                Case Enums.CharacterEncoding.UTF7

                    comb_ftp_server_encoding.SelectedIndex = 2

                Case Enums.CharacterEncoding.ASCII

                    comb_ftp_server_encoding.SelectedIndex = 3

                Case Else

                    comb_ftp_server_encoding.SelectedIndex = 0

            End Select

            chk_auth_required.Checked = _ftp_server.GetSetAuthRequired

            txt_ftp_server_user.Text = _ftp_server.GetSetUsername
            txt_ftp_server_password.Text = _ftp_server.GetSetPassword

            chk_use_special_character_fix.Checked = _ftp_server.GetSetUseSpecialCharacterFix

        Else
            txt_ftp_server_name.ReadOnly = False
            comb_ftp_server_transfer_mode.SelectedIndex = 0
            comb_ftp_server_mode.SelectedIndex = 1
            comb_ftp_server_encoding.SelectedIndex = 0
        End If





    End Sub

    Private Sub txt_ftp_server_host_TextChanged(sender As Object, e As EventArgs) Handles txt_ftp_server_host.TextChanged

        If String.IsNullOrEmpty(txt_ftp_server_host.Text) = False And txt_ftp_server_host.Text.StartsWith("ftp://") Then

            Try

                txt_ftp_server_host.Text = txt_ftp_server_host.Text.Replace("ftp://", "")

                If txt_ftp_server_host.Text.Contains("@") Then
                    chk_auth_required.Checked = True
                    txt_ftp_server_user.Text = txt_ftp_server_host.Text.Split(":")(0).ToString
                    txt_ftp_server_password.Text = txt_ftp_server_host.Text.Split(":")(1).ToString
                    txt_ftp_server_password.Text = txt_ftp_server_password.Text.Remove(txt_ftp_server_password.Text.IndexOf("@"))
                    txt_ftp_server_host.Text = txt_ftp_server_host.Text.Split("@")(1).ToString
                End If

                If txt_ftp_server_host.Text.Contains("/") Then
                    txt_ftp_server_path.Text = txt_ftp_server_host.Text.Remove(0, txt_ftp_server_host.Text.IndexOf("/"))
                    txt_ftp_server_host.Text = txt_ftp_server_host.Text.Remove(txt_ftp_server_host.Text.IndexOf("/"))
                End If

                If txt_ftp_server_host.Text.Contains("\") Then
                    txt_ftp_server_path.Text = txt_ftp_server_host.Text.Remove(0, txt_ftp_server_host.Text.IndexOf("\"))
                    txt_ftp_server_host.Text = txt_ftp_server_host.Text.Remove(txt_ftp_server_host.Text.IndexOf("\"))
                End If

                If txt_ftp_server_host.Text.Contains(":") Then
                    num_ftp_server_port.Value = Integer.Parse(txt_ftp_server_host.Text.Split(":")(1).ToString)
                    txt_ftp_server_host.Text = txt_ftp_server_host.Text.Remove(txt_ftp_server_host.Text.IndexOf(":"))
                End If

            Catch ex As Exception
            End Try

        End If

    End Sub

End Class